package com.hengxinguotong.baselibrary.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hengxinguotong.baselibrary.R;

/**
 * Created by hjz on 2018/3/15.
 */

public class ConfirmDialog extends AlertDialog
{
    private TextView confirmTitle;
    private TextView confirmContent;
    private TextView confirmCancel;
    private TextView confirmSure;

    private Builder builder;

    public ConfirmDialog(Builder builder)
    {
        this(builder.context);
        this.builder = builder;
    }

    public ConfirmDialog(@NonNull Context context)
    {
        super(context, R.style.dialog_no_action_bar);
    }

    public ConfirmDialog(@NonNull Context context, int themeResId)
    {
        super(context, themeResId);
    }

    protected ConfirmDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener)
    {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_confirm);

        confirmTitle = findViewById(R.id.confirm_title);
        confirmTitle.setText(builder.titleId);
        confirmContent = findViewById(R.id.confirm_content);
        confirmContent.setText(builder.contentId);
        confirmCancel = findViewById(R.id.confirm_cancel);
        confirmSure = findViewById(R.id.confirm_sure);
        confirmSure.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dismiss();
                if (builder.listener != null)
                {
                    builder.listener.onAccept(builder.data);
                }
            }
        });
        confirmCancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dismiss();
                if (builder.listener != null)
                {
                    builder.listener.onCancel();
                }
            }
        });
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
    }

    public interface OnConfirmListener<T>
    {
        void onAccept(T t);

        void onCancel();
    }

    public static class Builder<T>
    {
        private Context context;
        private int titleId;
        private int contentId;
        private T data;
        private OnConfirmListener<T> listener;

        public Builder(Context context)
        {
            this.context = context;
        }

        public Builder<T> title(int titleId)
        {
            this.titleId = titleId;
            return this;
        }

        public Builder<T> content(int contentId)
        {
            this.contentId = contentId;
            return this;
        }

        public Builder<T> data(T data)
        {
            this.data = data;
            return this;
        }

        public Builder<T> listener(OnConfirmListener listener)
        {
            this.listener = listener;
            return this;
        }

        public ConfirmDialog build()
        {
            return new ConfirmDialog(this);
        }
    }
}
